﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model;
using System.Text;
using HZ.Supervise.IBLL;
using System.Data;
namespace HZ.Supervise.Web.Secondphase.ReportHospital
{
    public partial class SubsidyDetailShow : BasePage
    {
        HZ.Supervise.IBLL.IBLL_Prescription iPrescription = HZ.Supervise.BLLFactory.IBl_Prescription();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                banding();

            }
        }
        //绑定未补助的医院
        private void banding()
        {
            string strSubsidyID = "";
            strSubsidyID = Request.QueryString["id"].ToString();
            string id = Request.QueryString["id1"].ToString();
            if (!id.Equals("0"))
            {
                btnDelete.Visible = false;
            }
            if (!string.IsNullOrEmpty(strSubsidyID))
            {
                int totalrecord = 0;
                txtSubsidyID.Text = strSubsidyID;
                HZ.Data.QueryModel qm = new HZ.Data.QueryModel();
                string strwhere = "1=1";
               // strwhere += string.Format("and SubsidyID= '{0}' ", strSubsidyID);
         
                if (!txtPrescriptionName.Text.Trim().Equals(""))
                {
                    strwhere += "and PrescriptionName like '%"+txtPrescriptionName.Text.Trim()+"%'";
                }
                if (!txtPdoctorName.Text.Trim().Equals(""))
                {
                     strwhere += "and PdoctorName like '%"+txtPdoctorName.Text.Trim()+"%'";
                }
                if (!txtPatientName.Text.Trim().Equals(""))
                {
                     strwhere += "and PatientName like '%"+txtPatientName.Text.Trim()+"%'";
                }
                qm["Append"] = strwhere;
                qm["SubsidyID"] = strSubsidyID;
                qm["SortFields"] = "  SubsidyDetailId ASC";
                AspNetPager1.RecordCount = totalrecord;
                this.gvwDetail.DataSource = iPrescription.GetSubsidyDetailTable(qm, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalrecord);
                this.gvwDetail.DataBind();
            }

        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            banding();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvwDetail.Rows.Count > 0)
            {

                string id = Request["checkbox1"];
                if (!string.IsNullOrEmpty(id))
                {
                    IList<HZ.Supervise.Model.SubsidyDetail> SubsidyDetailList = new List<HZ.Supervise.Model.SubsidyDetail>();
                    HZ.Supervise.Model.DrugPreWarning drugwar = new DrugPreWarning();
                    HZ.Supervise.IBLL.IBLL_PreWarning pre = HZ.Supervise.BLLFactory.IBLL_PreWarning();
                    string[] arraryIds = id.Split(',');
                    for (int i = 0; i < arraryIds.Length; i++)
                    {
                        HZ.Supervise.Model.SubsidyDetail item = new SubsidyDetail();
                        item.SubsidyID = arraryIds[i].Split('|')[0];
                        item.SubsidyDetailId = arraryIds[i].Split('|')[1];
                        item.PrescriptionId = arraryIds[i].Split('|')[2];
                        SubsidyDetailList.Add(item);
                    }
                    if (SubsidyDetailList.Count > 0)
                    {
                        HZ.Supervise.IBLL.IBLL_Prescription iPrescription = HZ.Supervise.BLLFactory.IBl_Prescription();
                        if (iPrescription.DeleteSubsidyDetailBacth(SubsidyDetailList) == 1)
                        {
                            HZ.Web.WebUtil.Alert("删除成功！");
                        }
                        else
                        {
                            HZ.Web.WebUtil.Alert("删除失败！");
                        }
                    }
                }
                else
                {
                    HZ.Web.WebUtil.Alert("请选择审核数据！");
                }
            }
            banding();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwDetail_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gvwDetail.EditIndex = e.NewEditIndex;
            banding();
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwDetail_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (Page.IsValid)
            {
                int rowIndex = e.RowIndex;
                string strSubsidyDetailId = gvwDetail.DataKeys[rowIndex].Value.ToString();
                string strNewCount = ((TextBox)gvwDetail.Rows[rowIndex].FindControl("txtNewCount")).Text.Trim();
                decimal Count =Convert.ToDecimal(gvwDetail.Rows[rowIndex].Cells[6].Text);
                decimal newCount = Convert.ToDecimal(strNewCount);
                if (Count<newCount)
                {
                     HZ.Web.WebUtil.Alert("已补助金额不能大于应补助金额！", this);
                     return;
                }
                if (string.IsNullOrEmpty(strNewCount))
                {
                    HZ.Web.WebUtil.Alert("补助金额不能为空！", this);
                    ((TextBox)gvwDetail.Rows[rowIndex].FindControl("txtNewCount")).Focus();
                    return;
                }
                HZ.Supervise.Model.SubsidyDetail newSubsidyDetail = new SubsidyDetail();
                IList<HZ.Supervise.Model.SubsidyDetail> newSubsidyDetailList = new List<SubsidyDetail>();
                newSubsidyDetail.SubsidyDetailId = strSubsidyDetailId;
                string strRmark = ((TextBox)gvwDetail.Rows[rowIndex].FindControl("txtNewBZ")).Text.Trim();
                if (!string.IsNullOrEmpty(strRmark))
                {
                    newSubsidyDetail.PRemark = strRmark;
                }
                else
                {
                    newSubsidyDetail.PRemark = " ";
                }
                newSubsidyDetail.PSbsidyAmount = Convert.ToDecimal(strNewCount);
                newSubsidyDetailList.Add(newSubsidyDetail);
                if (iPrescription.UpdateSubsidyDetailBacth(newSubsidyDetailList) == 1)
                {
                    //重新绑定列表
                    gvwDetail.EditIndex = -1;
                    banding();
                    HZ.Web.WebUtil.Alert("更新成功！", this);
                }
                else
                {
                    PageError("更新失败！", "");
                }
            }

        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwDetail_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.gvwDetail.EditIndex = -1;
            banding();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddSubsidyDetail.aspx?id=" + txtSubsidyID.Text.Trim());
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            banding();
        }



    }
}
